<?php

namespace app\home\controller;


use app\common\validate\merchant\MerchantOrderPointValid;
use app\common\validate\merchant\MerchantProductPointValid;
use app\home\loader\Services;
use think\Exception;

/**
 * 积分商城
 */
class Point extends ApiBase
{


    /**
     * 店铺入口
     * @return void
     */
    public function shop_entry(): void
    {
        $mer_id = input('get.key');
        try {
            if (empty($mer_id)){
                throw  new Exception('经销商不存在或已禁用,请联系经销商',API_ERROR);
            }
            $mer_id = base64_de($mer_id);
            Services::Point()->registerEntry($mer_id);
        } catch (Exception $e) {
            api_result(API_ERROR,'授权失败');

        }
    }

    /**
     * 登录
     * @return void
     */
    public function login()
    {
        $params = $this->request->post();
        try {
            if (empty($params['code'])) {
                api_exception(400, 'code不能为空');
            }
            if (empty($params['mkey'])) {
                api_exception(400, '店铺未找到');
            }
            $result = Services::Point()->wxLogin($params['code'], $params['mkey'], (int)$this->request_app);
            api_result(API_SUCCESS, '', $result);
        } catch (Exception $e) {
            api_result(API_ERROR, $e->getMessage());
        }
    }


    /**
     *  首页轮播图
     */
    public function banner_list()
    {
        try {
            $where[] = ['mer_id', '=', $this->merId];
            $where[] = ['status', '=', 1];
            $res = Services::Point()->bannerList($where);
            api_result(API_SUCCESS, 'OK', $res);
        } catch (Exception $e) {
            api_result($e);
        }
    }

    /**
     *  商品分页列表
     */
    public function product_page()
    {

        $data = input('get.');
        try {
            $where[] = ['mer_id', '=', $this->merId];
            if (!empty($data['class_id'])) {
                $where[] = ['class_id', '=', $data['class_id']];
            }
            $where[] = ['delete_at' , '=' , null];
            $where[] = ['status' , '=' , 1];

            if (!empty($data['type'])){
                $where[] = ['point', '<' , $this->shopUser['point']];
            }
            if (!empty($data['is_recommend'])){
                $where[] = ['is_recommend', '=' , $data['is_recommend']];
            }

            $options = [
                'where' => $where,
                'page' => $data['page'] ?: 1,
                'page_size' => $data['page_size'] ?: 10,
                'order' => 'id desc',
                'field' => 'id,title,sub_title,class_id,pics,point,stock_num,show_num,price',
            ];
            $res = Services::Point()->productPage($options);
            api_result(API_SUCCESS, 'OK', $res);
        } catch (Exception $e) {
            api_result($e);
        }
    }

    /**
     *  商品详情
     */
    public function product_info()
    {

        $options = input('get.');
        try {
            $this->validate($options, MerchantProductPointValid::class . '.product_info');
            $where = [
                'mer_id' => $this->merId,
                'id'  => $options['id'],
                'status'  => 1,
            ];
            $res = Services::Point()->productInfo($where,$this->merId);
            api_result(API_SUCCESS, 'OK', $res);
        } catch (Exception $e) {
            api_result($e);
        }
    }

    /**
     *  商品分类
     */
    public function class_list()
    {
        $options = input('get.');
        try {
            $where[] = ['mer_id', '=', $this->merId];
            $options['where'] = $where;
            $res = Services::Point()->classList($options);
            api_result(API_SUCCESS, 'OK', $res);
        } catch (Exception $e) {
            api_result($e);
        }
    }


    /**
     *  添加订单
     */
    public function order_add()
    {
        $data = input('post.');
        try {
            $this->validate($data, MerchantOrderPointValid::class . '.order_add');
            $data = [
                'product_code' => $data['product_code'],
                'product_num' => $data['product_num'],
                'address_id' => $data['address_id'],
                'mer_id' => $this->merId,
                'user_id' => $this->shopUser['user_id'],
                'user_type' => USER_TYPE_SALESMAN,
            ];
            $order_no = Services::Point()->orderAdd($data);
            api_result(API_SUCCESS, '添加成功', ['order_no' => $order_no]);
        } catch (Exception $e) {
            api_result($e);
        }
    }


    /**
     * 订单修改地址
     */
    public function order_save_address()
    {
        $data = input('post.');
        try {
            $this->validate($data, MerchantOrderPointValid::class . '.order_save_address');
            $data = [
                'order_no' => $data['order_no'],
                'address_id' => $data['address_id'],
                'mer_id' => $this->merId,
                'user_id' => $this->shopUser['user_id'],
                'user_type' => USER_TYPE_SALESMAN,
            ];
            $order_no = Services::Point()->orderSaveAddress($data);
            api_result(API_SUCCESS, '添加成功', ['order_no' => $order_no]);
        } catch (Exception $e) {
            api_result($e);
        }
    }


    /**
     *  提交订单
     */
    public function order_submit()
    {
        $data = input('post.');
        try {
            $this->validate($data, MerchantOrderPointValid::class . '.order_submit');
            $result =  Services::Point()->orderSubmit(
                $data['order_no'],
                $this->shopUser['user_id'],
            );
            api_result(API_SUCCESS, '下单成功', $result);
        } catch (Exception $e) {
            api_result($e);
        }
    }



    /**
     *  订单管理列表
     */
    public function order_page()
    {

        $data = input('get.');
        try {
            $where[] = ['mer_id','=',$this->merId];
            $where[] = ['user_id','=', $this->shopUser['user_id']];
            if (isset($data['status']) && $data['status'] !== 'all') {
                if (in_array($data['status'],[ORDER_STATUS_PENDING, ORDER_STATUS_WAIT_GOODS, ORDER_STATUS_SEND_GOODS , ORDER_STATUS_SUCCESS], true)){
                    throw new Exception('状态未找到');
                }
                $where[] = ['status', '=', (int)$data['status']];
            }
            $options = [
                'where' => $where,
                'page' => $data['page'] ?: 1,
                'page_size' => $data['page_size'] ?: 10,
                'order' => 'create_at desc',
                'filed' => 'order_no,product_code,product_title,product_num,sum_point,real_name,mobile,express_company,address,express_no,status,create_at',
            ];
            $res = Services::Point()->orderPage($options,$this->merId);
            api_result(API_SUCCESS, 'OK', $res);
        } catch (Exception $e) {
            api_result($e);
        }
    }


    /**
     *  订单详情
     */
    public function order_info()
    {
        $data = input('get.');
        try {
            $this->validate($data, MerchantOrderPointValid::class . '.order_info');
            $where = [
                'mer_id' => $this->merId,
                'user_id' => $this->shopUser['user_id'],
                'order_no' => $data['order_no']
            ];
            $res = Services::Point()->orderInfo($where);
            api_result(API_SUCCESS, 'OK', $res);
        } catch (Exception $e) {
            api_result($e);
        }
    }

    /**
     * 订单提醒
     */
    public function order_remind()
    {
        try {
            $res = Services::Point()->orderRemind(
                $this->shopUser['user_id'],
                $this->merId
            );
            api_result(API_SUCCESS, 'OK', $res);
        } catch (Exception $e) {
            api_result($e);
        }
    }


    /**
     * 订单取消
     */
    public function order_cancel()
    {
        $data = input('post.');
        try {
            $this->validate($data, MerchantOrderPointValid::class . '.order_info');
            $res = Services::Point()->orderCancel(
                $this->shopUser['user_id'],
                $this->merId,
                $data['order_no'],
            );
            api_result(API_SUCCESS, 'OK', $res);
        } catch (Exception $e) {
            api_result($e);
        }
    }

    /**
     * 订单确认
     */
    public function order_finish()
    {
        $data = input('post.');
        try {
            $this->validate($data, MerchantOrderPointValid::class . '.order_finish');
            $res = Services::Point()->orderFinish(
                $this->shopUser['user_id'],
                $this->merId,
                $data['order_no'],
            );
            api_result(API_SUCCESS, 'OK', $res);
        } catch (Exception $e) {
            api_result($e);
        }
    }

    /**
     * 积分列表
     */
    public function point_page()
    {
        $data = input('get.');
        try {
            $options = [
                'where' => ['mer_id' => $this->merId],
                'page' => $data['page'] ?: 1,
                'page_size' => $data['page_size'] ?: 10,
                'order' => 'id desc',
                'field' => 'id,point,re_point,create_at,memo',
            ];
            $options['where']['salesman_id'] = $this->shopUser['user_id'];
            $res = Services::Point()->pointPage($options);
            api_result(API_SUCCESS, 'OK', $res);
        } catch (Exception $e) {
            api_result($e);
        }
    }
}
